package com.iwayin.core.bo.like;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.iwayin.core.bo.EmailServiceUtil;
import com.iwayin.core.dao.ask.AskDAO;
import com.iwayin.core.dao.follow.FollowDAO;
import com.iwayin.core.dao.like.LikeDAO;
import com.iwayin.core.dao.setting.EmailSettingDAO;
import com.iwayin.core.dao.user.UserDAO;
import com.iwayin.core.dataobject.AskDO;
import com.iwayin.core.dataobject.EmailSettingDO;
import com.iwayin.core.dataobject.FollowDO;
import com.iwayin.core.dataobject.LikeDO;
import com.iwayin.core.dataobject.UserDO;
import com.iwayin.core.utils.exception.BOException;

public class LikeBOImpl implements LikeBO {
	private LikeDAO likeDAO;
	private UserDAO userDAO;
	private AskDAO askDAO;
	private FollowDAO followDAO;
	private EmailSettingDAO emailSettingDAO;

	@Override
	public void addLike(LikeDO like) {
		try {
			boolean isLike = this.likeDAO.isLike(like.getAskId(),like.getUserId());
			if (!isLike) {
				this.likeDAO.addLike(like);
				// 喜欢数加1
				AskDO ask = this.askDAO.findAskByAskId(like.getAskId());
				this.userDAO.likeReceivePlusOne(ask.getUserId());
				this.askDAO.likeCountPlusOne(like.getAskId());
				//通知ask人
				//喜欢自己的不发送邮件通知，提醒也不必记录
				if (like.getUserId().equals(ask.getUserId())) {
					return;
				}
				EmailSettingDO es = emailSettingDAO.getEmailSettingDO(ask.getId());
				if(es != null && 
						EmailSettingDO.USER_EMAIL_NOTIFICATION_IMMEDIATELY.equals(es.getLikes())){
					UserDO asker = userDAO.getEmailReceiver(ask.getUserId());
					Map<String, Object> param = new HashMap<String, Object>();
					UserDO liker = userDAO.getUserById(like.getUserId());
					param.put("likerName", liker.getNickname());
					param.put("likerAvatar", liker.getAvatarPath());
					param.put("likerId", like.getUserId().toString());
					param.put("askInfo", ask.getQuestion());
					param.put("askId", like.getAskId().toString());
					EmailServiceUtil.sendLikerEmail(asker.getLoginName(), param);
				}
			}
		} catch (Exception e) {
			throw new BOException(e);
		}
	}

	@Override
	public void deleteLike(LikeDO like) {
		try {
			this.likeDAO.deleteLike(like);
			// 喜欢数减1
			AskDO ask = this.askDAO.findAskByAskId(like.getAskId());
			this.userDAO.likeReceiveMinusOne(ask.getUserId());
			this.askDAO.likeCountMinusOne(like.getAskId());
		} catch (Exception e) {
			throw new BOException(e);
		}
	}

	@Override
	public List<UserDO> findAskLikeUsers(LikeDO like) {
		List<Long> userIds = this.likeDAO.findAskLikeUsers(like);
		List<UserDO> users = new ArrayList<UserDO>();
		if (userIds != null) {
			for (Long userId : userIds) {
				UserDO user = this.userDAO.getUserById(userId);
				FollowDO isFollow = followDAO.getFollower(like.getUserId(),user.getId());
				// 判断与当前登录用户关系
				user.setFollow(isFollow == null ? false : true);
				if (user != null) {
					users.add(user);
				}
			}
		}
		return users;
	}

	public void setLikeDAO(LikeDAO likeDAO) {
		this.likeDAO = likeDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setAskDAO(AskDAO askDAO) {
		this.askDAO = askDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

	public void setEmailSettingDAO(EmailSettingDAO emailSettingDAO) {
		this.emailSettingDAO = emailSettingDAO;
	}
	
}
